<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;

class Banner extends  Model
{
    use HasFactory;
    use SoftDeletes;

    static $unguarded = true;

    protected $table = 'banner';

    /**
     * 根据ID获取数据
     *
     * @param $id
     * @param mixed $columns
     * @return array|Builder
     */
    public static function getDataByID($id, string $columns = ''){
        $data = self::query();
        if($columns){
            $data->find($id,$columns);
            return $data;
        }
        $val = $data->find($id);
        $status_show =  getStatusValue($val['status']);
//        $status_html_show = getStatusHTMLValue($val['status']);
        $image = $val['image'];
        if($image){
            $image_path = Storage::url($val['image']);
            $image_show = '<img width="60" height="60" src="'.$image_path.'">';
        }
        return [
            'id'=>$val['id'],
            'name'=>$val['name'],
            'image'=>$image,
            'image_path'=>$image_path??"",
            'image_show'=>$image_show??"未上传",
            'status'=>$val['status'],
            'sort'=>$val['sort'],
            'status_show'=>$status_show,
//            'status_html_show'=>$status_html_show,
            'crated_at'=>Carbon::parse($val['crated_at'])->toDateTimeString(),
            'updated_at'=>Carbon::parse($val['updated_at'])->toDateTimeString(),
       ];
    }

}
